<template>
  <div class="dashboard-container employees-detail-container">
    <div class="app-container">
      <div class="dashboard-container">
        <div class="app-container">
          <el-card>
            <el-tabs>
              <el-tab-pane label="登录账户设置">
                <!-- 放置表单 -->
                <el-form ref="form" :model="userInfo" label-width="100px" style="margin-left: 120px; margin-top:30px" :rules="rules">
                  <el-form-item label="姓名:" prop="username">
                    <el-input v-model="userInfo.username" style="width:300px" />
                  </el-form-item>
                  <el-form-item label="密码:" prop="newPassword">
                    <el-input v-model="userInfo.newPassword" style="width:300px" />
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="update">更新</el-button>
                    <el-button @click="$router.back()">取消</el-button>
                  </el-form-item>
                </el-form>
              </el-tab-pane>

              <el-tab-pane label="个人详情">
                <!-- 打印按钮 -->
                <el-tooltip class="tooltip-box" content="打印基本个人信息">
                  <router-link :to="`/employees/print/${id}?type=personal`">
                    <i class="el-icon-printer" />
                  </router-link>
                </el-tooltip>
                <!--  个人详情组件 -->
                <user-info />
              </el-tab-pane>
              <el-tab-pane label="岗位信息">
                <!--打印按钮 -->
                <el-tooltip class="tooltip-box" content="打印基本岗位信息">
                  <router-link :to="`/employees/print/${id}?type=job`">
                    <i class="el-icon-printer" />
                  </router-link>
                </el-tooltip>
                <!-- 岗位信息组件 -->
                <job-info />
              </el-tab-pane>
            </el-tabs>
          </el-card>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// 获取员工个人信息
import { getUserById } from '@/api/user'
// 更新员工信息
import { saveUserDetailById } from '@/api/employees'
// 导入个人详情组件
import UserInfo from './components/userInfo'
import JobInfo from './components/jobinfo'
export default {
  name: 'EmployeeDetail',
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      // 存个人用户信息
      userInfo: {
        username: '',
        // 写新密码
        newPassword: ''
      },

      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: ['change', 'blur'] }
        ],
        newPassword: [
          { min: 6, max: 9, message: '密码长度6-9位', trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  computed: {
    id() {
      // 拿到用户id
      return this.$route.params.id
    }
  },

  created() {
    // 获取员工个人信息
    this.getUserInfo()
  },

  methods: {
    async getUserInfo() {
      // 发送请求，获取员工个人信息
      const res = await getUserById(this.id)
      console.log(res)
      this.userInfo = {
        ...this.userInfo,
        ...res
      }
    },

    // 更新员工信息
    async  update() {
      try {
        await this.$refs.form.validate()
      } catch (error) {
        return
      }
      await saveUserDetailById({
        ...this.userInfo,
        password: this.userInfo.newPassword
      })

      // 让对应账号的名字更新
      if (this.$store.state.user.userInfo.userId === this.id) {
        console.log(1111)
        this.$store.commit('user/setUserInfo', {
          ...this.$store.state.user.userInfo,
          username: this.userInfo.username
        })
        // this.$store.dispatch('user/getUserInfo')
      }
      this.$message.success('更新成功')
      this.$router.back()
    }
  }
}
</script>

<style lang="scss" scoped>
.employees-detail-container {
  .el-tab-pane {
    padding-top: 10px;
  }
  .tooltip-box {
    position: absolute;
    right: 30px;
    top: 10px;
    z-index: 999;
  }
}
</style>
